#ifndef VSASYNLOADER_H
#define VSASYNLOADER_H
#include "VSThread.h"
#include "VSSafeQueue.h"
#include "VSResourceManager.h"
namespace VSEngine2
{

	class VSGRAPHIC_API VSResourceJob : public VSMemObject
	{
	public:
		enum //Load state
		{
			LS_PENDING,
			LS_LOADED,
			LS_FAIL,
		};
		enum // JOB TYPE
		{
			JT_TEXTURE,
			JT_MATERIAL,
			JT_ANIMSET,
			JT_STATIC_MESH,
			JT_SKELECTON_MESH,
			JT_VSHADER,
			JT_PSHADER,
		};
		VSResourceJob();
		virtual ~VSResourceJob() = 0;
		unsigned int m_uiLoadState;
		unsigned int m_uiJobType;
	};
	 
	class VSGRAPHIC_API VSTextureRJob : public VSResourceJob
	{
	public:
		VSTextureRJob(VSTexAllStateRPtr& pProxy);
		virtual ~VSTextureRJob();
		VSTexAllStateRPtr m_pProxy;
		VSTexAllStatePtr m_pNewResource;
		bool m_bEngineType;
		bool m_bCompress;
		unsigned int m_uiCompressType;
		bool m_bIsNormal;
		bool m_bIsSoftSRGB;
		VSSamplerStatePtr	m_pSamplerState;
		VSFileName  m_FileName;
	};
	class VSGRAPHIC_API VSMaterialRJob : public VSResourceJob
	{
	public:
		VSMaterialRJob(VSMaterialRPtr& pProxy);
		virtual ~VSMaterialRJob();
		VSMaterialRPtr m_pProxy;
		VSMaterialPtr m_pNewResource;
		VSFileName  m_FileName;
	}; 
	class VSGRAPHIC_API VSAnimSetRJob : public VSResourceJob
	{
	public:
		VSAnimSetRJob(VSAnimSetRPtr& pProxy);
		virtual ~VSAnimSetRJob();
		VSAnimSetRPtr m_pProxy;
		VSAnimSetPtr m_pNewResource;
		VSFileName  m_FileName;
	}; 
	class VSGRAPHIC_API VSStaticMeshRJob : public VSResourceJob
	{
	public:
		VSStaticMeshRJob(VSStaticMeshNodeRPtr& pProxy);
		virtual ~VSStaticMeshRJob();
		VSStaticMeshNodeRPtr m_pProxy;
		VSStaticMeshNodePtr m_pNewResource;
		VSFileName  m_FileName;
	}; 
	class VSGRAPHIC_API VSSkelectonMeshRJob : public VSResourceJob
	{
	public:
		VSSkelectonMeshRJob(VSSkelectonMeshNodeRPtr& pProxy);
		virtual ~VSSkelectonMeshRJob();
		VSSkelectonMeshNodeRPtr m_pProxy;
		VSSkelectonMeshNodePtr m_pNewResource;
		VSFileName  m_FileName;
	}; 
	class VSGRAPHIC_API VSVShaderRJob : public VSResourceJob
	{
	public:
		VSVShaderRJob(VSVShaderRPtr& pProxy);
		virtual ~VSVShaderRJob();
		VSVShaderRPtr m_pProxy;
		VSVShaderPtr m_pNewResource;
		MaterialShaderPara1 m_Para;
		unsigned int uiPassType;
		unsigned int uiShaderID;
	}; 
	class VSGRAPHIC_API VSPShaderRJob : public VSResourceJob
	{
	public:
		VSPShaderRJob(VSPShaderRPtr& pProxy);
		virtual ~VSPShaderRJob();
		VSPShaderRPtr m_pProxy;
		VSPShaderPtr m_pNewResource;
		MaterialShaderPara1 m_Para;
		unsigned int uiPassType;
		unsigned int uiShaderID;
	}; 
	class VSGRAPHIC_API VSResourceLoaderThread : public VSThread
	{
	public:
		VSResourceLoaderThread();
		virtual ~VSResourceLoaderThread();
		void AddJob(VSResourceJob* m_pJob);
		void Stop();
	protected:
		virtual const TCHAR* GetThreadName()
		{
			return _T("VSResourceLoaderThread");
		}
		VSSafeQueue<VSResourceJob*> m_pResourceQueue;
		virtual void Run();	
		void LoadTexture(VSTextureRJob * pTextureRJob);
		void LoadMaterial(VSMaterialRJob * pMaterialRJob);
		void LoadStaticMesh(VSStaticMeshRJob * pStaicMeshRJob);
		void LoadSkelectonMesh(VSSkelectonMeshRJob * pSkelectonMeshRJob);
		void LoadAnimSet(VSAnimSetRJob * pAnimSetRJob);
		void LoadVShader(VSVShaderRJob * pVShaderRJob);
		void LoadPShader(VSPShaderRJob * pPShaderRJob);
	};
	
	class VSGRAPHIC_API VSASYNLoadManager
	{
	public:
		VSASYNLoadManager();
		~VSASYNLoadManager();
		void Update(double AppTime);
		void AddTextureLoad(VSTexAllStateRPtr& pTexture,VSFileName & FileName,
							bool bEngineType = true,
							VSSamplerStatePtr	pSamplerState = NULL,					
							bool bCompress = false,
							unsigned int uiCompressType = 0,
							bool bIsNormal = false,
							bool bIsSoftSRGB = false);
		void AddMaterial(VSMaterialRPtr& pMaterial,VSFileName & FileName);
		void AddAnimSet(VSAnimSetRPtr& pAnimSet,VSFileName & FileName);
		void AddStaticMesh(VSStaticMeshNodeRPtr& pStaticMesh,VSFileName & FileName);
		void AddSkelectonMesh(VSSkelectonMeshNodeRPtr& pSkelectonMesh,VSFileName & FileName);
		void AddVShader(VSVShaderRPtr& pVShader,MaterialShaderPara1 &MSPara1,unsigned int uiPassType,unsigned int uiShaderID);
		void AddPShader(VSPShaderRPtr& pPShader,MaterialShaderPara1 &MSPara1,unsigned int uiPassType,unsigned int uiShaderID);
		static VSASYNLoadManager * ms_pASYNLoadManager;
	protected:
		VSResourceLoaderThread m_ResourceLoadThread;
		VSArray<VSResourceJob * > m_ResourceJobArray;
		void ProcessTexture(VSTextureRJob * pTextureRJob);
		void ProcessMaterial(VSMaterialRJob * pMaterialRJob);
		void ProcessAnimSet(VSAnimSetRJob * pAnimSetRJob);
		void ProcessStaticMesh(VSStaticMeshRJob * pStaticMeshRJob);
		void ProcessSkelectonMesh(VSSkelectonMeshRJob * pSkelectonMeshRJob);
		void ProcessVShader(VSVShaderRJob * pVShaderRJob);
		void ProcessPShader(VSPShaderRJob * pPShaderRJob);
	};
}
#endif